class EventServer:
    def __init__(self):
        self.funcs = {}
        
    def register(self, cmd, func):
        if not self.funcs.has_key(cmd):
            self.funcs[cmd] = [func]
        else:
            self.funcs[cmd].append(func)
            
    def registerToFront(self, cmd, func):
        if not self.funcs.has_key(cmd):
            self.funcs[cmd] = [func]
        else:
            self.funcs[cmd].insert(0,func)
        
    def registerToBack(self, cmd, func):
        self.register(cmd,func)
            
    def unregister(self, cmd, func):
        if self.funcs.has_key(cmd) and func in self.funcs[cmd]:
            self.funcs[cmd].remove(func)
            if not self.funcs[cmd]:
                self.funcs.pop(cmd)
            return True
        return False
        
    def pull(self, cmd, args):
        if self.funcs.has_key(cmd):
            for func in self.funcs[cmd]:
                func(cmd,args)
                
    def unregisterObject(self, obj):
        #print "unregister", obj.getVar("name")
        for funcName, funcList in self.funcs.iteritems():
            removeList = []
            for func in funcList:
                if hasattr(func,'im_self') and func.im_self == obj:
                    #print "remove", funcName, func, func.im_self.getVar("name")
                    #funcList.remove(func)
                    removeList.append(func)
            [funcList.remove(func) for func in removeList]

eventServer = EventServer()
